package com.briup.es.web.controller;

import com.briup.es.config.response.Result;
import com.briup.es.service.UploadService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/upload")
@RequiredArgsConstructor
public class UploadController {
    private final UploadService uploadService;

    /**
     * 单个文件上传，上传成功后，返回该文件在
     * 网络上的唯一地址，即返回该文件的URL地址
     * 请求中携带的文件，必须通过 @RequestPart指定获取，
     * 然后通过 MultipartFile类型接收数据
     * @return Result<String> 成功响应，返回文件的URL地址
     */
    @PostMapping
    public Result<String> upload(@RequestPart MultipartFile file){
        String url = uploadService.upload(file);
        return Result.success(url);
    }
}
